﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Text;

namespace MyPS.CommonLib
{
    public static class MyPSExtention
    {
        /// <summary>
        /// object向string类型的转换,如果oject为null,返回空
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string NullString(this object o)
        {
            if (o == DBNull.Value || o == null)
            {
                return "";
            }
            else
            {
                return o.ToString();
            }
        }
        public static string DateTimeFormatString(this object o, string format)
        {
            if (o == null)
            {
                return "";
            }
            else
            {
                return ((DateTime)o).ToString(format);
            }
        }
        public static MvcHtmlString FileUploadFor<TModel, TValue>(this HtmlHelper<TModel> htmlhelper, Expression<Func<TModel, TValue>> expression)
        {
            string modelName = ExpressionHelper.GetExpressionText(expression);//从Lambda表达式中获取模型对应属性的名称
            ModelMetadata meta = ModelMetadata.FromLambdaExpression(expression, htmlhelper.ViewData);

            TagBuilder tagBuilder = new TagBuilder("input");//设置标签类型为input 
            tagBuilder.Attributes.Add("type", "text");//为标签添加type属性及值
            tagBuilder.Attributes.Add("name", modelName);//为标签添加name属性及值
            //tagBuilder.Attributes.Add("onclick", "JTC.setday({readOnly :true})");
            tagBuilder.Attributes.Add("value", meta.Model == null ? "" : meta.Model.ToString());
            tagBuilder.GenerateId(modelName);//为标签生成Id，name参数代码Id的值
            tagBuilder.MergeAttributes(htmlhelper.GetUnobtrusiveValidationAttributes(modelName, meta));
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));
            sbHtml.Append("<input type='button' class='btn' value='浏览...' />");
            sbHtml.Append(string.Format("<input type=\"file\" name=\"fileImgUrl\" class=\"file\" id=\"fileImgUrl\" size=\"28\" onchange=\"document.getElementById('{0}').value=this.value\" />", modelName));
            //创建经过HTML加密的字符串                                                                                                                                                                                                     
            //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的 
            return MvcHtmlString.Create(sbHtml.ToString());
        }

        public static MvcHtmlString DropdownListYNFor<TModel, TValue>(this HtmlHelper<TModel> htmlhelper, Expression<Func<TModel, TValue>> expression)
        {
            string modelName = ExpressionHelper.GetExpressionText(expression);//从Lambda表达式中获取模型对应属性的名称
            ModelMetadata meta = ModelMetadata.FromLambdaExpression(expression, htmlhelper.ViewData);
            TagBuilder tagBuilder = new TagBuilder("select");
            tagBuilder.Attributes.Add("name", modelName);
            var value = meta.Model.NullString().Trim().ToUpper();
            switch (value)
            {
                case "":
                    tagBuilder.InnerHtml = "<option value='' selected></option><option value='Y'>是</option><option value='N'>否</option>";
                    break;
                case "Y":
                    tagBuilder.InnerHtml = "<option value=''></option><option value='Y' selected>是</option><option value='N'>否</option>";
                    break;
                case "N":
                    tagBuilder.InnerHtml = "<option value=''></option><option value='Y' >是</option><option value='N' selected>否</option>";
                    break;
            }
            tagBuilder.GenerateId(modelName);
            tagBuilder.MergeAttributes(htmlhelper.GetUnobtrusiveValidationAttributes(modelName, meta));
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString UEditorFor<TModel, TValue>(this HtmlHelper<TModel> htmlhelper, Expression<Func<TModel, TValue>> expression)
        {
            string modelName = ExpressionHelper.GetExpressionText(expression);//从Lambda表达式中获取模型对应属性的名称
            ModelMetadata meta = ModelMetadata.FromLambdaExpression(expression, htmlhelper.ViewData);
            StringBuilder sb_html = new StringBuilder();
            sb_html.Append("<link href=\"~/ueditor1_2_6_0-utf8-net/themes/iframe.css\" type=\"text/css\" />");
            sb_html.Append("<script type=\"text/javascript\" src=\"/UEdit/ueditor.config.js\"></script>");
            sb_html.Append("<script type=\"text/javascript\" src=\"/UEdit/ueditor.all.min.js\"></script>");
            TagBuilder tagBuilder = new TagBuilder("script");//设置标签类型为input 
            tagBuilder.Attributes.Add("type", "text/plain");//为标签添加type属性及值
            tagBuilder.Attributes.Add("name", modelName);//为标签添加name属性及值
            //tagBuilder.Attributes.Add("onclick", "JTC.setday({readOnly :true})");
            tagBuilder.InnerHtml = meta.Model == null ? "" : meta.Model.ToString();
            tagBuilder.GenerateId(modelName);//为标签生成Id，name参数代码Id的值
            tagBuilder.MergeAttributes(htmlhelper.GetUnobtrusiveValidationAttributes(modelName, meta));
            sb_html.Append(tagBuilder.ToString(TagRenderMode.Normal));
            sb_html.Append(string.Format(@"<script type='text/javascript'>$(document).ready(function(){0}var editor = UE.getEditor('{1}', {2} initialFrameHeight: 280, initialFrameWidth: 800 {3});{4})</script>", "{", modelName, "{", "}", "}"));
            return MvcHtmlString.Create(sb_html.ToString());
        }
    }

}